
#include "gelu_tiling.h"
#include "register/op_def_registry.h"
#include "tiling_common.h"
namespace optiling {
static ge::graphStatus TilingFunc(gert::TilingContext *context) {
  GeluTilingData tiling;
  auto size = set_size_by_input(tiling, context, 0);
  COMMON_TILING_GET_PLATFORM;
  COMMON_TILING_GET_UB_SIZE;
  auto dtype = context->GetInputDesc(0)->GetDataType();
  auto dtypeSize = GetSizeByDataType(dtype);

  context->SetBlockDim(1);

  // 分割UB
  // x * 2, y * 2;  2 + 2 = 4;
  // 需要一个tempB
  // 4 + 1 = 5
  auto count = 5;
  if (dtype == ge::DataType::DT_FLOAT) {
    // TEMP1
    count += 1;
  }
  auto split_ub = ub_size / count / 256 * 256 / dtypeSize;
  tiling.set_size(size);
  tiling.set_tileLength(split_ub);
  tiling.set_finalLength(size % split_ub);
  std::cout << "size = " << tiling.get_size() << std::endl;
  std::cout << "tileLength = " << tiling.get_tileLength() << std::endl;
  std::cout << "finalLength = " << tiling.get_finalLength() << std::endl;
  tiling_zero_workspace(context);
  tiling_save_to_buffer(tiling, context);
  return ge::GRAPH_SUCCESS;
}
} // namespace optiling

namespace ge {
static ge::graphStatus InferShape(gert::InferShapeContext *context) {
  const gert::Shape *x1_shape = context->GetInputShape(0);
  gert::Shape *y_shape = context->GetOutputShape(0);
  *y_shape = *x1_shape;
  return GRAPH_SUCCESS;
}
} // namespace ge

namespace ops {
class Gelu : public OpDef {
public:
  explicit Gelu(const char *name) : OpDef(name) {
    this->Input("x")
        .ParamType(REQUIRED)
        .DataType({ge::DT_FLOAT16, ge::DT_FLOAT})
        .Format({ge::FORMAT_ND, ge::FORMAT_ND})
        .UnknownShapeFormat({ge::FORMAT_ND, ge::FORMAT_ND});
    this->Output("y")
        .ParamType(REQUIRED)
        .DataType({ge::DT_FLOAT16, ge::DT_FLOAT})
        .Format({ge::FORMAT_ND, ge::FORMAT_ND})
        .UnknownShapeFormat({ge::FORMAT_ND, ge::FORMAT_ND});

    this->SetInferShape(ge::InferShape);

    this->AICore().SetTiling(optiling::TilingFunc);
    this->AICore().AddConfig("ascend310b");
  }
};

OP_ADD(Gelu);
} // namespace ops
